﻿using System.Windows;
using Microsoft.Win32;
using WinAPI;
using TaskbarHelper;

namespace TiledShell.Windows
{
    /// <summary>
    /// Interaction logic for ShutDownWindow.xaml
    /// </summary>
    public partial class ShutDownWindow : Window
    {
        RegistryKey logonkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon");
        public ShutDownWindow()
        {
            InitializeComponent();
            var value = logonkey.GetValue("Shell");
            DefaultLabel.Content = "Current: " + value ;
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            Application.Current.Shutdown();
        }

        private void ShutDown_Click(object sender, RoutedEventArgs e)
        {
            WinAPI.Shutdown.ExitWindowsEx(WinAPI.Shutdown.ExitWindows.ShutDown & Shutdown.ExitWindows.ForceIfHung, WinAPI.Shutdown.ShutdownReason.MajorOther & WinAPI.Shutdown.ShutdownReason.MinorOther);
        }

        private void SwapDefault_Click(object sender, RoutedEventArgs e)
        {
            var value = logonkey.GetValue("Shell");
            logonkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", true);
            if (value.ToString().Substring(0, 1) == "e")
            {
                string path;
                path = System.Reflection.Assembly.GetExecutingAssembly().Location;
               logonkey.SetValue("Shell", path);
            }
            else
            {
                logonkey.SetValue("Shell", "explorer.exe");
            }
            logonkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon");
            value = logonkey.GetValue("Shell");
            DefaultLabel.Content = "Current: " + value;
        }
    }
}
